package org.bite.lesson04;

/**
 * 执行不同对象的synchronized方法， 修改全局变量
 *
 * @Author kiku-wa
 */
public class Demo404 {
    public static void main(String[] args) throws InterruptedException {
        Counter404 c1 = new Counter404();
        Counter404 c2 = new Counter404();
        Thread t1 = new Thread(()->{
            for(int i = 0; i < 50000; i++) {
                c1.increase();
            }
        });
        Thread t2 = new Thread(()->{
            for(int i = 0; i < 50000; i++) {
                c2.increase();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(Counter404.count);
    }
}
class Counter404 {
    public static int count;
    //锁对象不同，存在线程安全问题
    //加上一个static使得锁对象为同一个，解决
    public  synchronized void increase() {
        count++;
    }
}
